package time.after.time.web;


import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springside.modules.security.springsecurity.SpringSecurityUtils;
import org.springside.modules.utils.web.struts2.Struts2Utils;
import time.after.time.entity.account.User;
import time.after.time.service.account.AccountManager;
import time.after.time.service.account.UserDetailsServiceImpl;
import time.after.time.sys.statics.PasswordHandler;


@Action(results={ 
		@Result(name = "index", location = "/!", type = "redirect"),
		@Result(name ="my", location = "/my/!", type = "redirect"),
		
		@Result(name ="pro", location = "/pro/!", type = "redirect"),
		
		@Result(name ="users", location = "/account/user", type = "redirect")
})
public class LoginAction extends BaseActionSupport {

	private static final long serialVersionUID = 1L;
	private String userName;
	private String passwd;
	@Autowired
	private AccountManager accountManager;
	
	@Override
	public String execute() {
		System.out.println("login!!!");
		return "index";
	}

	public void domainLogin()
	{
		User user=accountManager.findUserByLoginName(userName);
		String juPanPwd=PasswordHandler.getSaltSourceCode(passwd);
		if(user==null)//初次使用域账户，初始化用户资料
		{
			Struts2Utils.renderJson(new time.after.time.sys.utils.Result(500,"无效的用户名"));
			return;
		}
		else if(!juPanPwd.equals(user.getPassword()))
		{
			Struts2Utils.renderJson(new time.after.time.sys.utils.Result(500,"密码错误"));
			return;
		}
		Struts2Utils.renderJson(new time.after.time.sys.utils.Result(200));
	}
	
	/**
	 * 登录成功处理
	 * @return
	 */
	public String loginSuccess() 
	{
		String loginName=SpringSecurityUtils.getCurrentUserName();
		session.setAttribute("ME",UserDetailsServiceImpl.getUserIdByName(loginName));
		System.out.println("loginSuccess!!!");
		return "pro";
	}

	public String offLine() {
		System.out.println("offLine!!!");
		return "index";
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public void setPasswd(String passwd) {
		this.passwd = passwd;
	}	
}
